package com.zxn.xiaoran.view;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.zxn.presenter.view.BaseDialogFragment;
import com.zxn.xiaoran.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * AttributesEditDgFrag
 * Created by zxn on 2019-6-12 10:19:34.
 */
public class CommonDgFrag extends BaseDialogFragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.tv_dialog_title)
    TextView tvDialogTitle;
    @BindView(R.id.tv_dialog_content)
    TextView tvDialogContent;
    @BindView(R.id.v_line_h)
    View vLineH;
    @BindView(R.id.v_line_v)
    View vLineV;
    @BindView(R.id.tv_dialog_cancle)
    TextView tvDialogCancle;
    @BindView(R.id.tv_dialog_ok)
    TextView tvDialogOk;


    private String mTitle;
    private String mContent;


    public CommonDgFrag() {
    }

    public static CommonDgFrag newInstance(String title, String content, OnDialogClickListener listener) {
        CommonDgFrag fragment = new CommonDgFrag();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, title);
        args.putString(ARG_PARAM2, content);
        fragment.setArguments(args);
        fragment.mOnDialogClickListener = listener;
        return fragment;
    }

    /**
     * 默认有取消和确定按钮.
     *
     * @param listener
     * @return
     */
    public static CommonDgFrag newInstance(IDialogCreate listener) {
        CommonDgFrag fragment = new CommonDgFrag();
        fragment.mDialogEntity = listener;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTitle = getArguments().getString(ARG_PARAM1);
            mContent = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected int initTheme() {
        return R.style.NiceDialogStyle;
    }

//    @Override
//    protected void onDialogCreated(WindowManager.LayoutParams params, Dialog dialog) {
//        super.onDialogCreated(params, dialog);
//        params.gravity = Gravity.CENTER;
//        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
//    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_common_dg;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (null != mDialogEntity) {
            tvDialogTitle.setText(mDialogEntity.getTitle());
            tvDialogContent.setText(mDialogEntity.getContent());

            tvDialogCancle.setVisibility((mDialogEntity.isSingleButton() ? View.GONE : View.VISIBLE));
            vLineV.setVisibility((mDialogEntity.isSingleButton() ? View.GONE : View.VISIBLE));
        } else {
            tvDialogTitle.setText(mTitle);
            tvDialogContent.setText(mContent);
        }

    }
    private boolean isConfirm;

    @OnClick({R.id.tv_dialog_cancle, R.id.tv_dialog_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_dialog_cancle:
                isConfirm = false;
                dismiss();
                break;
            case R.id.tv_dialog_ok:
                if (null != mOnDialogClickListener) {
                    isConfirm = true;
                    mOnDialogClickListener.onConfirmClick(view);
                }

                if (null != mDialogEntity) {
                    mDialogEntity.onConfirmClick(view);
                }
                dismiss();
                break;
        }
    }

    private OnDialogClickListener mOnDialogClickListener;


    private IDialogCreate mDialogEntity;

    public interface IDialogCreate extends OnDialogClickListener {

        String getTitle();

        String getContent();

        boolean isSingleButton();
    }

}
